<?php
include_once(dirname(__FILE__).'/../services.direct.php');
include_once(dirname(__FILE__).'/../services.product.php');

// ExtDirect Wrapper for services.product

class DirectProduct extends DirectService
{

    private $svc = null;
    
    public function __construct()
    {
        $this->svc = new Product();
    }

    /**
     *@remotable
     *@formHandler
    **/
    public function updateProduct( $form )
    {
        check_admin_rights();
        
        try
        {
			if( isset( $form['new_tag'] ) )
			{
		
				if( $form['new_tag'] == 1 )
				{
					$id_tag = $this->svc->createProduct( $form['id_tag'], $form['esl_tag_alias'],
						$form['esl_tag_barcode'], $form['esl_tag_id_producto'] );
				}
				else
				{
					$id_tag = $this->svc->updateProduct( $form['id_tag'], $form['esl_tag_alias'],
						$form['esl_tag_barcode'], $form['esl_tag_id_producto'] );
				}
					
				return array( 'success' => true, 'id_tag' => $id_tag );			
			}
        }
        catch(Exception $e)
        {
            $this->getLogger()->debug( print_r( $e, true ) );
            return array( 'success' => false, 
            	'msg' => 'Se ha producido un error al intentar crear o editar el producto.' );
        }
    }
    
    /**
     *@remotable
     *@formHandler
    **/
    public function linkProduct($form)
    {
        check_admin_rights();
        
        try
        {
			$id_tag = $this->svc->linkProduct( $form['id_tag'], $form['esl_tag_id_producto'] );
			return array( 'success' => true, 'id_tag' => $id_tag );
        }
        catch(Exception $e)
        {
            $this->getLogger()->debug( print_r( $e, true ) );
            return array( 'success' => false, 
            	'msg' => 'Se ha producido un error al intentar crear o editar el producto.' );
        }
    }
    
    /**
     *@remotable
    **/
    public function deleteProduct( $params )
    {
        check_admin_rights();
        try
        {
            $result = $this->svc->deleteProduct( $params->id_tag );
            return array( 'success' => true );
        }
        catch(Exception $e)
        {
            $this->getLogger()->debug( print_r( $e,true ) );
            return array( 'success'=>false, 
            	'message'=>'Se ha producido un error al intentar eliminar el producto seleccionado.' );
        }
    }
	
	/**
     *@remotable
    **/
    public function listTags( $params )
    {
        $result = $this->svc->listTags( (array)$params );
        return array( 'success' => true, 'rows' => $result['rows'], 'results' => $result['results'] );
    }
	
	/**
     *@remotable
    **/
    public function listProducts( $params )
    {
        $result = $this->svc->listProducts((array)$params);
        return array( 'success' => true, 'rows' => $result['rows'], 'results' => $result['results'] );
    }

	/**
     *@remotable
    **/
	public function updateDisplaysInfo()
	{
        $result = $this->svc->updateDisplaysInfo();
        return array( 'success' => true );
	}

}

?>
